/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "favsparser.h"

#include <QByteArray>

#include <QDebug>

#include "../Types/favorites.h"
#include "../Types/favorite.h"

char parserFav(const QByteArray& favsData, const int indexData, Favorite& f)
{
    int indexStartIdSong = favsData.indexOf("<a href=\"/demovibes/song/",indexData) + strlen("<a href=\"/demovibes/song/");
    int indexEndIdSong = favsData.indexOf("/\">",indexStartIdSong);
    int indexStartNameSong = indexEndIdSong + strlen("/\">");
    int indexEndNameSong = favsData.indexOf("</a>",indexStartNameSong);
    int indexStartArtist = indexEndNameSong;
    // +3 / -2 because we have nasty characters
    int indexStartLengthSong = favsData.indexOf("<td align=\"center\">",indexStartArtist) + strlen("<td align=\"center\">") + 3;
    int indexEndLengthSong = favsData.indexOf("</td>",indexStartLengthSong) - 2;
    int indexStartState = favsData.indexOf("<td align=\"center\">",indexEndLengthSong) + strlen("<td align=\"center\">") +3;
    int indexEndState = favsData.indexOf("</td>",indexStartState) -2;
    int indexStartVote = favsData.indexOf("<td>",indexEndState) + strlen("<td>") + 3;
    int indexEndVote = favsData.indexOf("</td>",indexStartVote) - 2;
    int indexStartLock= favsData.indexOf("title=\"",indexEndVote) + strlen("title=\"");
    int indexEndLock = favsData.indexOf("\" />", indexStartLock);
    int indexStartRequestDate = favsData.indexOf("<span class=\"lastreqdate\">",indexEndLock) + strlen("<span class=\"lastreqdate\">");
    int indexEndRequestDate = favsData.indexOf("</span>",indexStartRequestDate);

    f.song.id = favsData.mid(indexStartIdSong, indexEndIdSong - indexStartIdSong).toUInt();
    f.song.title = QString::fromUtf8(favsData.mid(indexStartNameSong, indexEndNameSong - indexStartNameSong));
    f.song.length = QTime::fromString(favsData.mid(indexStartLengthSong, indexEndLengthSong - indexStartLengthSong),"m:ss");


    // Search for artist
    while ( ( indexStartArtist = favsData.indexOf("<span>",indexStartArtist)) != -1 && indexStartArtist < indexEndRequestDate )
    {
        indexStartArtist += strlen("<span>");

        int indexStartFlag = favsData.indexOf("alt='flag' title='",indexStartArtist);

        int indexStartIdArtist = favsData.indexOf("<a href=\"/demovibes/artist/",indexStartArtist) + strlen("<a href=\"/demovibes/artist/");
        int indexEndIdArtist = favsData.indexOf("/\"",indexStartIdArtist);
        int indexStartNameArtist = favsData.indexOf(">",indexEndIdArtist) + strlen(">");
        int indexEndNameArtist = favsData.indexOf("</a>",indexStartNameArtist);

        Artist a;

        if ( indexStartFlag != -1 && indexStartFlag < indexEndRequestDate )
        {
            indexStartFlag += strlen("alt='flag' title='");
            int indexEndFlag = favsData.indexOf("' />",indexStartFlag);

            a.flag = favsData.mid(indexStartFlag, indexEndFlag - indexStartFlag);
        }
        else
        {
            a.flag = "";
        }

        a.id = favsData.mid(indexStartIdArtist, indexEndIdArtist - indexStartIdArtist).toUInt();
        a.nickname = QString::fromUtf8(favsData.mid(indexStartNameArtist, indexEndNameArtist - indexStartNameArtist));

        f.artists.push_back(a);
    }

    f.lastRequest = QDate::fromString(favsData.mid(indexStartRequestDate, indexEndRequestDate - indexStartRequestDate),"yyyy-MM-dd");
    f.status = statusFactory(favsData.mid(indexStartState, indexEndState - indexStartState));
    if ( favsData.mid(indexStartLock, indexEndLock - indexStartLock) == "Locked")
    {
        f.isRequestable = false;
    }
    else
    {
        f.isRequestable = true;
    }
    f.vote = favsData.mid(indexStartVote, indexEndVote-indexStartVote).toFloat();

    return 0;
}

bool Parser :: readPageFavs(const QByteArray& favsData, Favorites& favs)
{
    int indexSongFaved = 0;
    bool isRow2 = false;
    bool noError = true;
    QString keyString = "<tr class=\"row1\">";

    qDebug() << "Parser :: readPageFavs";

    if ( favsData.contains("Error</h2>") == true )
    {
        favs.favs.clear();
        qDebug() << "You are not allowed to se this page";
        return true;
    }

    if ( favsData.contains("Favourite Songs</h2>") == false )
    {
        qDebug() << "The page does not look like a favorite page";
        return false;
    }

    // Parse the name of the favorite owner
    int indexH2Tag = favsData.indexOf("<h2>");
    int indexStartOwnerName = favsData.indexOf("/>",indexH2Tag) + strlen("/>");
    int indexEndOwnerName = favsData.indexOf("'",indexStartOwnerName);
    favs.owner = QString::fromUtf8(favsData.mid(indexStartOwnerName, indexEndOwnerName - indexStartOwnerName));

    // Parse the number of page accessible
    int indexPaging = favsData.indexOf("<div class=\"paging tr\">");

    if ( indexPaging != -1 )
    {
        int indexStartPagingMaximum = favsData.indexOf("<div class=\"fl\">Page",indexPaging) + strlen("<div class=\"fl\">Page");
        indexStartPagingMaximum = favsData.indexOf("of ",indexStartPagingMaximum) + strlen("of ");
        int indexEndPagingMaximum = favsData.indexOf("</div>",indexStartPagingMaximum);

        favs.nbPages = favsData.mid(indexStartPagingMaximum, indexEndPagingMaximum - indexStartPagingMaximum).toInt(&noError);
    }
    else
    {
        favs.nbPages = 0;
    }

    while((indexSongFaved = favsData.indexOf(keyString,indexSongFaved)) != -1)
    {
        Favorite f;

        parserFav(favsData,indexSongFaved,f);
        favs.favs.push_back(f);

        indexSongFaved += strlen(keyString.toStdString().c_str());

        if ( isRow2 )
        {
            keyString = "<tr class=\"row2\">";
        }
        else
        {
            keyString = "<tr class=\"row1\">";
        }
        isRow2 = !isRow2;
    }

    qDebug() << favs;

    return noError;
}
